package cn.bjc._05_xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) throws Exception {
        //1.实例化生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("xhr_producer_group");

        //2.设置nameserver
        producer.setNamesrvAddr("localhost:9876");
        
        //3.启动
        producer.start();

        //4.创建消息对象,设置消息内容
        Message message = new Message("xhr", "hello", "消息发过来啦~".getBytes(StandardCharsets.UTF_8));
        
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("信息消费成功!" + sendResult.toString());
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("信息消费失败!");
            }
        });
        System.out.println("测试结束");
        
        //5.关闭
        //异步暂时不关
        
    }
    
    
    
    
}
